Page({
  data:{
    cards: [],
    contact: []
  },
  onLoad(event) {
    var that = this
    console.log(event)
    wx.cloud.callFunction({
      name:'queryAllCard',
      data: {
        start: event.start,
        end: event.end,
        offDate: event.offDate,
        offTime: event.offTime,
        cap: parseInt(event.cap)
      },
      success: res=>{
        this.setData({
          cards: res.result.data
        })
        console.log("调用云函数查询卡片成功！")
      },
      fail: res=>{
        console.log("调用云函数查询卡片失败！！！")
      }
    })
  },
  showDetial(event) {
    var arr = []
    for(var i = 0; i < this.data.cards[event.currentTarget.id].phone.length; i++){
      var obj = {}
      var num = i + 1
      obj.name = '乘客' + num
      obj.subname = this.data.cards[event.currentTarget.id].phone[i]
      arr.push(obj)
    }
    console.log(obj)
    this.setData({
      show: event.currentTarget.id,
      contact: arr
    })
  },
  onClose() {
    this.setData({
      show: -1
    })
    console.log(this.data.show)
  },
  onJoin(event){
    console.log(event)
    var phone = wx.getStorageSync('phone')
    if(phone == '' || !phone) {
      wx.showModal({
        title: "添加失败",
        content: '您的手机号未填写，请在‘我的’页面填写后再试'
      })
    }
    const db = wx.cloud.database({
      env: 'cloud-dev-alpha-3f1k3'
    })
    db.collection('relation').add({  
      data: {
        cardID: event.currentTarget.dataset.cardid
      },
      success: res=>{
        console.log("创建关系记录成功！")
        wx.cloud.callFunction({
          name: 'joinGroup',
          data: {
            _id: event.currentTarget.id,
            phone: phone
          },
          success: res=>{
            console.log(res)
            if(res.result) {
              wx.showToast({
                title: '加入成功',
                icon: 'success',
                duration: 2000
              })
            }
            else{
              wx.showModal({
                title: '加入失败',
                content: '加入队伍失败，请重试'
              })
            }
          },
          fail: res=>{
            console.log("调用云函数失败！！！")
          }
        })
      },
      fail: res=>{
        console.log("创建关系记录失败！！！")
        // error = true
      }
    })
    
  }
})